﻿<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Common" %>
<%@ Import Namespace="Maticsoft.DBUtility" %>
<script runat=server>
    const string overlineidtag = "overlineid";
    const string wireidtag = "wireid";
    const string toweridtag = "towerid";
    const string deviceidtag = "deviceid";
    const string starttowertag = "starttower";
    const string finishtowertag = "finishtower";
    const string belonglinetag = "belongline";
    const string belongtowertag = "belongtower";
    const string longitudetag = "longitude";
    const string latitudetag = "latitude";
    const string elevationtag = "elevation";
    const string editabletag = "editable";
    const string devicenametag = "devicename";
    const string daoxiantag = "导线";
    const string gantatag = "杆塔";
    const string shuxingtag = "属性";
    const string xianlutag = "线路";
    const string shebeitag = "设备";
    const string columnnametag = "columnname";
    const string valuetag = "value";
    const string aliasnametag = "aliasname";
    const string statustag = "status";
    const string titletag = "title";
    const string shujuliebiaotag = "数据列表";
    const string xianluliebiaotag = "线路列表";
    const string gantaliebiaotag = "杆塔列表";
    private void createsqls(out string updatesql, out string insertsql, out string delsql, string tablename,string tableid,XmlElement xmlele)
    {
        updatesql = "update " + tablename + " set ";
        insertsql = "insert into " + tablename;
        string columns = "";
        string values = "";
        delsql = "";
        string id = getAtt(xmlele,tableid);
        delsql = "delete from " + tablename + " where " + tableid + " = " + id;
        XmlNodeList attnodes = xmlele.SelectNodes("属性");
        foreach (XmlNode attnode in attnodes)
        {
            XmlElement attele = attnode as XmlElement;
            string columnname = attele.GetAttribute("columnname");
            string value = attele.GetAttribute("value");
            updatesql += columnname + "='" + value + "',";
            columns += columnname + ",";
            values += "'" + value + "',";
        }
        updatesql = updatesql.TrimEnd(',');
        updatesql += " where " + tableid +"=" + id;
        columns = "(" + columns.TrimEnd(',') + ")";
        values = "(" + values.TrimEnd(',') + ")";
        insertsql += columns + " values " + values.Replace(id,"0");        
    }
    /// <summary>
    /// 获取线路、导线、设备的属性att的值
    /// </summary>
    /// <param name="xmlele">线路、导线、设备的dom节点</param>
    /// <param name="att">属性名 columnname</param>
    /// <returns></returns>
    private string getAtt(XmlNode xmlele, string att)
    {
        return xmlele.SelectSingleNode(shuxingtag + "[@" + columnnametag + "='" + att + "']/@" + valuetag).Value;
    }
    private void setAtt(XmlNode xmlnode, string att, string value)
    {
        xmlnode.SelectSingleNode(shuxingtag + "[@" + columnnametag + "='" + att + "']/@" + valuetag).Value = value;
    }
    /// <summary>
    /// 获取线路、导线、杆塔、设备的tagname为tagname的，columnname为att，值为value的dom节点
    /// </summary>
    /// <param name="xmlele"></param>
    /// <param name="att"></param>
    /// <param name=value></param>
    /// <returns></returns>
    private XmlNodeList SelectNodes(XmlNode xmlele, string tagname, string att, string value)
    {
        return xmlele.SelectNodes(tagname + "[@" + statustag + "!='-' and " + shuxingtag + "[@" + columnnametag + "='" + att + "'and @" + valuetag + "='" + value + "']]");
    }
</script>
<%
    System.IO.Stream stream = Request.InputStream;
    System.IO.StreamReader sr = new System.IO.StreamReader(stream);

    string content = sr.ReadToEnd();
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    doc.LoadXml(content);
    try
    {
        doc.Save(@"C:\" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond + ".xml");
    }
    catch (Exception ex)
    {

    }
    DbConnection connection = Maticsoft.DBUtility.DbHelper.CreateConnection();
    connection.Open();
    DbCommand command = connection.CreateCommand();
    System.Xml.XmlNodeList towernodes = doc.SelectNodes("//杆塔[@status!='=']");
    foreach (System.Xml.XmlNode towernode in towernodes)
    {
        XmlElement xmlele = towernode as XmlElement;
        string id = getAtt(xmlele,"towerid");
        string status = xmlele.GetAttribute("status");
        string updatesql, insertsql, delsql;
        createsqls(out updatesql, out insertsql, out delsql, "tab_tower", "towerid",xmlele);
        if (status == "*")
        {
            command.CommandText =  updatesql;
        }
        else if (status == "-")
        {
            command.CommandText = delsql;         
        }
        else if (status == "+")
        {
            command.CommandText = insertsql;            
        }
        try
        {
            command.ExecuteNonQuery();
            ///如果是新增的杆塔，那么需要将其插入后的ID取出来，把与之相关联的导线的finishtower改为该ID
            ///
            if (status == "+")
            {
                command.CommandText = "select S_TAB_TOWER.currval from dual";
                object obj = command.ExecuteScalar();
                if(obj != null){
                    int objid = int.Parse(obj.ToString());

                    XmlNodeList daoxiannodes = SelectNodes(doc, "//" + daoxiantag, starttowertag, id);
                    foreach(XmlNode daoxiannode in daoxiannodes){
                        setAtt(daoxiannode, starttowertag, objid.ToString());
                    }
                    daoxiannodes = SelectNodes(doc, "//" + daoxiantag, finishtowertag, id);
                    foreach (XmlNode daoxiannode in daoxiannodes)
                    {
                        setAtt(daoxiannode, finishtowertag, objid.ToString());
                    }
                    XmlNodeList shebeinodes = SelectNodes(doc, "//" + shebeitag, belongtowertag, id);
                    foreach (XmlNode shebeinode in shebeinodes)
                    {
                        setAtt(shebeinode, belongtowertag, objid.ToString());
                    }
                }
            }
        }
        catch (Exception ex)
        {
            
        }                
    }    
    XmlNodeList xianlunodes = doc.SelectNodes("//线路[@status!='=']");
    string lineids = "";
    foreach (XmlNode xianlunode in xianlunodes)
    {
        XmlElement xmlele = xianlunode as XmlElement;
        string status = xmlele.GetAttribute("status");
        string updatesql, insertsql, delsql;
        string id = getAtt(xmlele,overlineidtag);
        createsqls(out updatesql, out insertsql, out delsql, "tab_overline","overlineid", xmlele);
        if (status == "+")
        {
            command.CommandText = insertsql;            
        }
        else if (status == "-")
        {
            command.CommandText = delsql;       
        }
        else if (status == "*")
        {
            command.CommandText = updatesql;
            lineids += id + " ";
        }
        try
        {
            command.ExecuteNonQuery();
            ///如果是新增的杆塔，那么需要将其插入后的ID取出来，把与之相关联的导线的finishtower改为该ID
            ///
            if (status == "+")
            {
                command.CommandText = "select S_TAB_OVERLINE.currval from dual";
                object obj = command.ExecuteScalar();
                if (obj != null)
                {
                    int objid = int.Parse(obj.ToString());
                    lineids += objid.ToString() + " ";
                    XmlNodeList daoxiannodes = xianlunode.SelectNodes(".//导线");
                    foreach (XmlNode daoxiannode in daoxiannodes)
                    {
                        setAtt(daoxiannode, belonglinetag, objid.ToString());
                    }
                    XmlNodeList shebeinodes = xianlunode.SelectNodes(".//设备");
                    foreach (XmlNode shebeinode in shebeinodes)
                    {
                        setAtt(shebeinode, belonglinetag, objid.ToString());
                    }
                }
            }
            if (status == "-")
            {
                command.CommandText = "delete from tab_wire where belongline = " + id;
                command.ExecuteNonQuery();
                command.CommandText = "delete from tab_device where belongline = " + id;
                command.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {

        }        
    }
    xianlunodes = doc.SelectNodes("//线路[@status='=']");
    foreach (XmlNode xianlunode3 in xianlunodes)
    {
        lineids += getAtt(xianlunode3, overlineidtag) + " ";
    }
    XmlNodeList daoxiannodes1 = doc.SelectNodes("//线路[@status!='-']//导线[@status!='=']");
    foreach (XmlNode daoxianode in daoxiannodes1)
    {
        XmlElement xmlele = daoxianode as XmlElement;
        string status = xmlele.GetAttribute("status");
        string updatesql, insertsql, delsql;
        string id = getAtt(xmlele,wireidtag);
        createsqls(out updatesql, out insertsql, out delsql, "tab_wire", "wireid", xmlele);
        if (status == "+")
        {
            command.CommandText = insertsql;
        }
        else if (status == "-")
        {
            command.CommandText = delsql;
            XmlNodeList shebeinodes = xmlele.SelectNodes("设备");
            foreach (XmlNode shebeinode in shebeinodes)
            {
                XmlElement shebeiele = shebeinode as XmlElement;
                shebeiele.SetAttribute("status", "-");
            }
        }
        else if (status == "*")
        {
            command.CommandText = updatesql;
        }
        try
        {
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            
        }
    }
    XmlNodeList shebeinodes1 = doc.SelectNodes("//线路[@status!='-']//设备[@status!='=']");
    foreach (XmlNode shebeinode in shebeinodes1)
    {
        XmlElement xmlele = shebeinode as XmlElement;
        string status = xmlele.GetAttribute("status");
        string updatesql, insertsql, delsql;
        string id = getAtt(xmlele,deviceidtag);
        createsqls(out updatesql, out insertsql, out delsql, "tab_device", "deviceid", xmlele);
        if (status == "+")
        {
            command.CommandText = insertsql;
        }
        else if (status == "-")
        {
            command.CommandText = delsql;            
        }
        else if (status == "*")
        {
            command.CommandText = updatesql;
        }
        try
        {
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {

        }
    }
    if (lineids.Trim() == "")
    {
        lineids = "1.001 1.002";
    }
     %><%=lineids %>
